package com.spring.Log;

import com.spring.Entity.AdminEntity;
import com.spring.Entity.UserEntity;
import com.spring.Service.Interface.AdminService;
import com.spring.Service.Interface.LogService;
import com.spring.Service.Interface.UserServie;
import com.spring.Service.LogServiceImpl;
import org.aspectj.lang.JoinPoint;
import org.aspectj.lang.annotation.AfterReturning;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Before;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import java.time.LocalDateTime;

/**
 * Created by Arabira on 2016/8/15.
 */
@Aspect
@Component
public class UserLog {
    private static String Domain = "普通用户";


    @Autowired
    private UserServie userServie;
    @Autowired
    private LogService Log;

    @Before("execution(* com.spring.Controller.UserControllerImpl.testLogin(..))")
    public void login(JoinPoint joinPoint) {
        Object[] values = joinPoint.getArgs();
        UserEntity userEntity = (UserEntity) values[0];
        HttpServletRequest request = (HttpServletRequest) values[values.length - 1];
        Log.log(userEntity.getUserName(), userEntity.getId(), request.getRemoteAddr(), LocalDateTime.now(), Domain, "登录操作");
    }

    @Before("execution(* com.spring.Controller.UserControllerImpl.logOut(..))")
    public void logout(JoinPoint joinPoint) {
        Object[] value = joinPoint.getArgs();
        HttpSession session = (HttpSession) value[0];
        HttpServletRequest request = (HttpServletRequest) value[value.length - 1];
        String userId = (String) session.getAttribute("SID");
        if (!userId.equals("null")) {
            UserEntity userEntity = userServie.findById(userId);
            Log.log(userEntity.getUserName(), userId, request.getRemoteAddr(), LocalDateTime.now(), Domain, "登出操作");
        }
    }

//    @Before("execution(* com.spring.Controller.UserControllerImpl.addUserExcel(..))")
//    public void addExcel(JoinPoint joinPoint) {
//        Object[] values = joinPoint.getArgs();
//        MultipartFile file = (MultipartFile) values[0];
//        HttpServletRequest request = (HttpServletRequest) values[values.length - 1];
//        Log.log("", "", request.getRemoteAddr(), LocalDateTime.now(), Domain, "使用excel表格添加操作，文件为：" + file.getName());
//    }

    @Before("execution(* com.spring.Controller.UserControllerImpl.addUser(..))")
    public void add(JoinPoint joinPoint) {
        Object[] values = joinPoint.getArgs();
        UserEntity userEntity = (UserEntity) values[0];
        HttpServletRequest request = (HttpServletRequest) values[1];
        Log.log(userEntity.getUserName(), userEntity.getId(), request.getRemoteAddr(), LocalDateTime.now(), Domain, "添加用户操作");
    }

    @AfterReturning("execution(* com.spring.Controller.UserControllerImpl.updateUser(..))")
    public void update(JoinPoint joinPoint) {
        Object[] values = joinPoint.getArgs();
        HttpSession session = (HttpSession) values[0];
        HttpServletRequest request = (HttpServletRequest) values[values.length - 1];
        String userId = (String) session.getAttribute("SID");
        UserEntity userEntity = userServie.findById(userId);
        String function = "更新邮件地址为：OA：" + (String) values[1] + "  139：" + (String) values[2];
        Log.log(userEntity.getUserName(), userId, request.getRemoteAddr(), LocalDateTime.now(), Domain, function);
    }

    @Before("execution(* com.spring.Controller.UserControllerImpl.changePassword(..))")
    public void change(JoinPoint joinPoint) {
        Object[] values = joinPoint.getArgs();
        HttpSession session = (HttpSession) values[0];
        HttpServletRequest request = (HttpServletRequest) values[values.length - 1];
        String userId = (String) session.getAttribute("SID");
        UserEntity userEntity = userServie.findById(userId);
        Log.log(userEntity.getUserName(), userId, request.getRemoteAddr(), LocalDateTime.now(), Domain, "修改密码操作");
    }

    @Before("execution(* com.spring.Controller.UserControllerImpl.findById(..))")
    public void findById(JoinPoint joinPoint) {
        Object[] values = joinPoint.getArgs();
        HttpSession session = (HttpSession) values[0];
        HttpServletRequest request = (HttpServletRequest) values[values.length - 1];
        String userId = (String) session.getAttribute("SID");
        UserEntity userEntity = userServie.findById(userId);
        Log.log(userEntity.getUserName(), userId, request.getRemoteAddr(), LocalDateTime.now(), Domain, "查找用户信息");
    }

    @Before("execution(* com.spring.Controller.UserControllerImpl.findByUserName(..))")
    public void findByName(JoinPoint joinPoint) {
        Object[] values = joinPoint.getArgs();
        HttpServletRequest request = (HttpServletRequest) values[values.length - 1];
        HttpSession session = (HttpSession) values[1];
        UserEntity userEntity = new UserEntity();
        userEntity.setId((String) session.getAttribute("SID"));
        if (!userServie.checkUser(userEntity)) {
            Log.log(userEntity.getUserName(), userEntity.getId(), request.getRemoteAddr(), LocalDateTime.now(), Domain, "查找用户信息");
        } else {
            Log.log("", "", request.getRemoteAddr(), LocalDateTime.now(), Domain, "查找用户操作");
        }
    }

}

